<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Python data types</title>
<link rel="stylesheet" href="/cfg/format.css" type="text/css">
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="keywords" content="Python, data types, tutorial, programming language, learn Python">
<meta name="description" content="In this chapter of the Python tutorial, we will
cover Python data types.">
<meta name="language" content="en">
<meta name="author" content="Jan Bodnar">
<meta name="distribution" content="global">

<script type="text/javascript" src="/lib/jquery.js"></script>
<script type="text/javascript" src="/lib/common.js"></script>

</head>

<body>

<div class="container">

<div id="wide_ad" class="ltow">
<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* 160x600, August 2011 */
google_ad_slot = "2484182563";
google_ad_width = 160;
google_ad_height = 600;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>

<div class="content">

<a href="/" title="Home">Home</a>&nbsp;
<a href="..">Contents</a>


<h1>Python data types</h1>

<p>
In this part of the Python programming tutorial, we will talk about Python data types. 
</p>

<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* NewSquare */
google_ad_slot = "0364418177";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script> 
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> 
</script> 

<p>
Computer programs work with data. Spreadsheets, text editors, calculators 
or chat clients. Tools to work with various data types are essential part 
of a modern computer language.  According to the wikipedia definition, a 
<b>data type</b> is a set of values, and the allowable operations 
on those values.
</p>

<p>
Python has a great set of useful data types. Python's data types are built in the 
core of the language. They are easy to use and straightforward.
</p>


<h2>Boolean values</h2>

<p>
There is a duality built in our world. There is a Heaven and Earth, water and fire, 
jing and jang, man and woman, love and hatred. In python programming language, the 
Boolean datatype is a primitive datatype having one of two values: True or False. 
This is a fundamental data type. Very common in computer programs. Interesingly, 
this data type was not there from the beginning, but it was created later on. 
</p>

<p>
Happy parents are waiting a child to be born. They have chosen a name for both 
possibilities. If it is going to be a boy, they might have chosen John. If it is 
going to be a girl, they might have chosen Victoria. 
</p>

<pre class="code">
#!/usr/bin/python

# kid.py

import random

male = False
male = bool(random.randint(0, 1))

if (male):
   print "We will use name John"
else:
   print "We will use name Victoria"
</pre>

<p>
The script uses a random integer generator to simulate our case. 
</p>

<pre class="explanation">
 import random
</pre>

<p>
Here we import the random module, that is used to calculate random numbers. 
</p>

<pre class="explanation">
 male = bool(random.randint(0, 1))
</pre>

<p>
Here we use two functions. the <b>randint()</b> function returns 
a random number from the given integer boundaries. In our case 0 or 1. The 
<b>bool()</b> function converts the integers to boolean values.
</p>

<pre class="explanation">
if (male):
   print "We will use name John"
else:
   print "We will use name Victoria"
</pre>

<p>
We print the name. The if command works with boolean values. If the variale male is 
True, we print the "We will use name John" to the console. If it has a False value, 
we print the other string. 
</p>

<p>
The following script shows some common values that are considered to be True or False. 
</p>

<pre class="code">
#!/usr/bin/python

# boolean.py

print bool(True)
print bool(False)
print bool("text")
print bool("")
print bool(' ')
print bool(0)
print bool()
print bool(3)
print bool(None)
</pre>

<p>
In fact, in the previous example, we did not have to use the bool() function.
</p>

<pre>
$ ./boolean.py 
True
False
True
False
True
False
False
True
False
</pre>

<p>
This is the output of the boolean.py script. 
</p>


<h2>None</h2>

<p>
There is another special data type - <b>None</b>. Basically, 
the data type means non existent, not known or empty. 
</p>


<pre class="code">
#!/usr/bin/python

# None.py

def function():
   pass

print function()
</pre>

<p>
In our example, we define a funtion. Functions will be covered later in the tutorial. 
The function does nothing. It does not explicitly return any value. Such a function 
will implicitly return None object. 
</p>

<pre>
None
</pre>

<p>
The output of the None.py script. 
</p>

<h2>Numbers</h2>

<p>
I am not going to define what a number is. This is quite a philosophical question. 
Ask Pythagoras. I assume, we all know or have some notion what a number is. 
</p>

<p>
In Python programming language, we have integer numbers, floating point numbers and complex numbers. 
</p>

<p>
If we work with integers, we deal with discrete entities. We would use integers to count apples. 
</p>

<pre class="code">
#!/usr/bin/python

# apples.py

# number of baskets
baskets = 16

# number of apples in a basket
apples_in_basket = 24

# we get the total number of apples
total = baskets * apples_in_basket


print "There are total of", total, "apples"
</pre>


<p>
In our script, we count the total amount of apples. We use the multiplication operation. 
</p>

<pre>
$ ./apples.py 
There are total of 384 apples
</pre>

<p>
The output of the script. 
</p>

<p>
Floating point numbers represent real numbers in computing. Real numbers measure continuous quantities. 
Let's say a sprinter for 100m ran 9.87s. What is his speed in km/h?
</p>

<pre class="code">
#!/usr/bin/python

# sprinter.py

# 100m is 0.1 km

distance = 0.1

# 9.87s is 9.87/60*60 h

time = 9.87 / 3600

speed = distance / time

print "The average speed of" \
      " a sprinter is " , speed, "km/h"
</pre>

<p>
To get the speed, we divide the distance by the time. 
</p>

<pre class="explanation">
print "The average speed of" \
      " a sprinter is " , speed, "km/h"
</pre>

<p>
The \ character is called an escape character. The escape character changes 
the meaning of the new line character. We see the code in two lines, but the 
interpreter drops the new line character, and sees only one line. Without the 
escape character, the interpreter would complain about indentation.
</p>

<pre>
$ ./sprinter.py 
The average speed of a sprinter is  36.4741641337 km/h
</pre>

<p>
This is the output of the sprinter script. 36.4741641337 is a floating point number.
</p>

<h2>Strings</h2>

<p>
<b>String</b> is a data type representing textual data in computer 
programs. Probable the single most important data type in programming. 
</p>


<p>
Strings in python can be created using single quotes, double quotes and tripple quotes. 
When we use tripple quotes, strings can span several lines without using the escape character. 
</p>

<pre class="code">
#!/usr/bin/python

# strings.py

a = "proximity alert"
b = 'evacuation'
c = """
requiem 
for
a 
tower
"""

print a
print b
print c
</pre>

<p>
In our example we assign three string literals to a, b, c variables. And we print them to the console. 
</p>

<pre>
$ ./strings.py 
proximity alert
evacuation

requiem 
for 
a 
tower
</pre>

<p>
This is the output of the strings.py script. 
</p>

<p>
When we work with strings, we can use <b>escape sequences</b>. The escape sequences 
are special characters, that
have a specific purpose, when used within a string. 
</p>

<pre>
 print "   bbb\raaa" # prints aaabbb
</pre>

<p>
The carriage return \r is a control character for end of line return to beginning of line.
</p>

<pre class=code>
#!/usr/bin/python

# strophe.py

print "Incompatible, it don't matter though\n'cos someone's bound to hear my cry"
print "Speak out if you do\nYou're not easy to find"
</pre>

<p>
The new line is a control character, which begins a new line of text. 
</p>

<pre>
$ ./strophe.py 
Incompatible, it don't matter though
'cos someone's bound to hear my cry
Speak out if you do
You're not easy to find
</pre>

<p>
Next we examine the backspace control character. 
</p>

<pre>
print "Python\b\b\booo" # prints Pytooo
</pre>

<p>
The backspace control character \b moves the cursor one character back. In our 
case, we use three backspace characters
to delete three letters and replace them with three o characters. 
</p>

<pre>
print "Towering\tinferno" # prints Towering        inferno
</pre>

<p>
The horizontal tab puts a space between text. 
</p>

<pre>
"Johnie's dog"
'Johnie\'s dog'
</pre>

<p>
Single and double quotes can be nested. Or in case we use only single quotes, 
we can use the backslash to escape the
default meaning of a single quote. 
</p>

<pre>
print "eagle has", len("eagle"), "characters"
</pre>

<p>
We can use the <b>len()</b> function to calculate the length 
of the string in characters. 
</p>

<p>
If we append an r to the string, we get a raw string. The escape sequences 
are not interpreted.
</p>

<pre class="code">
#!/usr/bin/python

# raw.py

print r"Another world\n"
</pre>

<pre>
$ ./raw.py 
Another world\n
</pre>

<p>
We get the string with the lew line character included. 
</p>

<p>
In the next example, we will show string multiplication and concatenation. 
</p>

<pre class="code">
#!/usr/bin/python

# strings2.py

print "eagle " * 5

print "eagle " "falcon"

print "eagle " + "and " + "falcon"
</pre>

<p>
The * operation repeates the string n times. In our case five times. Two string 
literals next to each other are automatically concatenated. We can also use the + 
operator to explicitly concatenate the strings. 
</p>

<pre>
$ ./strings2.py 
eagle eagle eagle eagle eagle 
eagle falcon
eagle and falcon
</pre>

<p>
This is the output of the strings.py script. 
</p>


<p>
Python programming language has several built-in data types for working 
with collections of values. These are currently tuples, lists, sets and dictionaries. 
</p>

<h2>Tuples</h2>

<p>
A <b>tuple</b> is an immutable sequence data type. The 
tuple can contain mixed data types. 
<p>

<pre>
fruits = ("oranges", "apples", "bananas")
</pre>


<p>
Tuples are created using round brackets. Here we have a tuple consisting 
of three fruit types. 
</p>

<pre>
fruits = "apples", "oranges", "bananas"
print fruits  # prints  ('apples', 'oranges', 'bananas')
</pre>

<p>
The parentheses are not mandatory. We can omit them. 
</p>


<pre class="code">
#!/usr/bin/python

# tuples.py

first = (1, 2, 3)
second = (4, 5, 6)

print "len(first) : ", len(first)
print "max(first) : ", max(first)
print "min(first) : ", min(first)
print "first + second :", first + second
print "first * 3 : ", first * 3
print "1 in first : ", 1 in first
print "5 not in second : ", 5 not in second
</pre>

<p>
This example shows several basic operations with tuples. The 
<b>len()</b> function returns the number of elements in 
the first tuple. The <b>max()</b> function returns the 
maximum value, the <b>min()</b> minimum value. The addition 
operator adds two tuples, the multiplication operator multiplies the tuple. 
The <b>in</b> operator determines if the values is in the tuple. 
</p>

<pre>
$ ./tuples.py
len(first) :  3
max(first) :  3
min(first) :  1
first + second : (1, 2, 3, 4, 5, 6)
first * 3 :  (1, 2, 3, 1, 2, 3, 1, 2, 3)
1 in first :  True
5 not in second :  False
</pre>

<p>
This is the output of the script. 
</p>

<p>
Next will will do some indexing. 
</p>

<pre class="code">
#!/usr/bin/python

# tuples2.py

five = (1, 2, 3, 4, 5)

print "five[0] : ", five[0]
print "five[-1] : ", five[-1]
print "five[-2] : ", five[-2]
print "five[:] : ", five[:]
print "five[0:4] : ", five[0:4]
print "five[1:2] : ", five[1:2]
print "five[:2] : ", five[:2]
print "five[:-1] : ", five[:-1]
print "five[:9] : ", five[:9]
</pre>

<p>
To get a value from a tuple, we use square brackets []. Note that we count indexes 
from 0. If there are five objects in a tuple, the indexes are 0...4. If we use a 
negative index,  we get a value from the end of the tuple. So index -1 gets the 
last element, -2 gets the last but one element. Python enables to create <b>slices</b> 
from tuples. For this we use the : delimiter. [0:4] gives (1, 2, 3, 4). Note that 
the last element is not included. We can omit one or both indexes
in a slice. It [:4] gives (1, 2, 3, 4). It goes from the first element. [0:] 
gives (1, 2, 3, 4, 5). This time, the last element is included. If we go out of 
bounds, we simply get all elements in the tuple. 
</p>

<pre>
$ ./tuples2.py 
five[0] :  1
five[-1] :  5
five[-2] :  4
five[:] :  (1, 2, 3, 4, 5)
five[0:4] :  (1, 2, 3, 4)
five[1:2] :  (2,)
five[:2] :  (1, 2)
five[:-1] :  (1, 2, 3, 4)
five[:9] :  (1, 2, 3, 4, 5)
</pre>

<p>
The output. 
</p>


<p>
Tuples can contain several mix data types. 
</p>


<pre class="code">
#!/usr/bin/python

# mix.py

mix = (1, 2, "solaris", (1, 2, 3))

print "mix[1] :", mix[1]
print "mix[2] :", mix[2]
print "mix[3] :", mix[3]
print "mix[3][0] :", mix[3][0]
print "mix[3][1] :", mix[3][1]
print "mix[3][2] :", mix[3][2]
</pre>


<p>
In our example, we have put numbers, string and a tuple into the mix tuple. 
</p>


<pre>
$ ./mix.py 
mix[1] : 2
mix[2] : solaris
mix[3] : (1, 2, 3)
mix[3][0] : 1
mix[3][1] : 2
mix[3][2] : 3
</pre>


<p>
To get the elements from the nested tuple, we use two square brackets. 
</p>

<p>
We have an exception when we work with tuples containing one element. 
Parentheses are also used in expressions. How do we distinguish between an expression and a one element tuple?
The creators of the python programming language decided to use a comma to denote, that we are using a tuple. 
</p>

<pre class="code">
#!/usr/bin/python

# onetuple.py

print (3 + 7)
print (3 + 7, )
</pre>

<p>
In the first case we have and expression. We print number 10 to the console. In the second case we deal with
a tuple. We print a tuple containing number 10. 
</p>

<pre>
$ ./onetuple.py 
10
(10,)
</pre>

<p>
Output. 
</p>


<h2>Lists</h2>

<p>
A <b>list</b> is a mutable sequence data type. The list can contain mixed data types. 
A list and a tuple share many common features. Because a list is a 
modifiable data type, it has some additional operations. A whole chapter is
dedicated to the Python list. 
<p>

<pre>
actors = ["Jack Nicholson", "Antony Hopkins", "Adrien Brody"]
</pre>

<p>
The list is created using the square brackests [].
</p>


<pre class="code">
#!/usr/bin/python

# list.py

num = [0, 2, 5, 4, 6, 7]

print num[0]
print num[2:]
print len(num)
print num + [8, 9]
</pre>

<p>
As we have stated previously, we can use the same operations on lists as on tuples. 
</p>

<pre>
$ ./list.py 
0
[5, 4, 6, 7]
6
[0, 2, 5, 4, 6, 7, 8, 9]
</pre>

<p>
Output. 
</p>

<p>
Next we will concentrate on additional operations we can do on lists. For example sorting. 
</p>


<pre class="code">
#!/usr/bin/python

# sort.py

numbers = [4, 3, 6, 1, 2, 0, 5 ]

print numbers
numbers.sort()
print numbers
</pre>

<p>
In our script we have a list of numbers. To sort those numbers, we use the built-in 
<b>sort()</b> function. 
</p>

<pre>
$ ./sort.py 
[4, 3, 6, 1, 2, 0, 5]
[0, 1, 2, 3, 4, 5, 6]
</pre>

<p>
The <b>reverse()</b> function will sort the elements of a list in reverse order. 
</p>

<pre>
numbers.reverse()   #  [5, 4, 3, 2, 1, 0]
</pre>

<p>
Counting elements in a list is done with the <b>count()</b> method. 
</p>

<pre class="code">
#!/usr/bin/python

# counting.py

numbers = [0, 0, 2, 3, 3, 3, 3]

print "zero is here ",  numbers.count(0), "times" 
print "one is here ",   numbers.count(1), "times" 
print "two is here ",   numbers.count(2), "time" 
print "three is here ", numbers.count(3), "times" 
</pre>

<p>
The script counts number occurences in a list. 
</p>

<pre>
$ ./counting.py 
zero is here  2 times
one is here  0 times
two is here  1 time
three is here  4 times
</pre>


<p>
Next we will deal with inserting and deleting items from the list. 
</p>


<pre class="code">
#!/usr/bin/python

# modify.py

names = []

names.append("Frank")
names.append("Alexis")
names.append("Erika")
names.append("Ludmila")

print names
names.insert(0, "Adriana")
print names
names.remove("Frank")
names.remove("Alexis")
del names[1]
print names
del names[0]
print names
</pre>


<p>
In our example we first create an empty names list. We use the 
<b>append()</b> function to append new items to the list. 
The elements are appended in the consecutive way. The <b>insert()</b> function 
inserts new elements at a given position. The existing elements are not deleted, they are moved. 
The <b>remove()</b> function removes a specific item from the list. 
If we want to delete an item based on the index, we use the <b>del</b> keyword. 
</p>

<pre>
$ ./modify.py 
['Frank', 'Alexis', 'Erika', 'Ludmila']
['Adriana', 'Frank', 'Alexis', 'Erika', 'Ludmila']
['Adriana', 'Ludmila']
['Ludmila']
</pre>

<p>
This is the output of the modify.py script. 
</p>

<p>
There are other ways, how we can modify a list. 
</p>

<pre class="code">
#!/usr/bin/python

# modify2.py

first = [1, 2, 3]
second = [4, 5, 6]

first.extend(second)
print first

first[0] = 11
first[1] = 22
first[2] = 33
print first

print first.pop(5)
print first
</pre>

<p>
The <b>extend()</b> method appends a whole list to another list. To 
modify an element in a list, we can use the assignment operator. 
The <b>pop()</b> method takes an item from the list and returns it. 
</p>

<pre>
$ ./modify2.py 
[1, 2, 3, 4, 5, 6]
[11, 22, 33, 4, 5, 6]
6
[11, 22, 33, 4, 5]
</pre>

<p>
Output. 
</p>

<p>
In the following example, we will find out indexes of elements. 
</p>

<pre class="code">
#!/usr/bin/python

# index.py

numbers = [0, 1, 2, 3, 3, 4, 5]

print numbers.index(1)
print numbers.index(3)
</pre>

<p>
To find an index in a list, we use the <b>index()</b> method. 
If there are more occurences of an element,
the method returns the index of the first element. 
</p>

<pre>
$ ./index.py 
1
3
</pre>

<p>
Output of the index.py script. 
</p>

<p>
Next we will do some transformations.
</p>

<pre class="code">
#!/usr/bin/python

# tuli.py

first = [1, 2, 3]
second = (4, 5, 6)

print tuple(first)
print list(second)

print first
print second
</pre>

<p>
We can use <b>tuple()</b> function to create a tuple from a list and <b>list()</b>
function to create a list from a tuple. Note, that the original objects are not 
modified, the functions only return those
transformed collections. 
</p>

<pre>
$ ./tuli.py 
(1, 2, 3)
[4, 5, 6]
[1, 2, 3]
(4, 5, 6)
</pre>


<h2>Sets</h2>

<p>
A <b>set</b> is an unordered collection of data with no duplicate elements. 
A set supports operations like union, intersection or difference. Similar as in Mathematics. 
</p>

<pre class="code">
#!/usr/bin/python

# sets.py

set1 = set(['a', 'b', 'c', 'c', 'd'])
set2 = set(['a', 'b', 'x', 'y', 'z'])

print "set1: " , set1
print "set2: " , set2
print "intersection: ", set1 &amp; set2
print "union: ", set1 | set2
print "difference: ", set1 - set2
print "symmetric difference: ", set1 ^ set2
</pre>

<p>
In our example we have two sets. We use the <b>set()</b> function to create sets. 
The intersection operation returns elements that are both in set1 and set2. 
The union operation returns all elements from both sets. The difference returns 
elements that are in the set1 but not is set2. And finally, the symmetric difference 
returns elements that are in set1 or set2, but not in both. 
</p>

<pre>
$ ./sets.py 
set1:  set(['a', 'c', 'b', 'd'])
set2:  set(['a', 'x', 'b', 'y', 'z'])
intersection:  set(['a', 'b'])
union:  set(['a', 'c', 'b', 'd', 'y', 'x', 'z'])
difference:  set(['c', 'd'])
symmetric difference:  set(['c', 'd', 'y', 'x', 'z'])
</pre>

<p>
This is the output of the sets.py script.
</p>

<p>
Next we introduce some other operations with sets. 
</p>


<pre class="code">
#!/usr/bin/python

# sets2.py

set1 = set([1, 2])
set1.add(3)
set1.add(4)

set2 = set([1, 2, 3, 4, 6, 7, 8])
set2.remove(8)

print set1
print set2

print "Is set1 subset of set2 ? : ", set1.issubset(set2)
print "Is set1 superset of set2 ? : ", set1.issuperset(set2)

set1.clear()
print set1
</pre>

<p>
The <b>add()</b> method adds an item to the set. The  <b>remove()</b> item removes an
item from the set. The  <b>clear()</b> method removes all items from the set. 
set1 is superset of set2 if every element in set2 is also in set1. set1 is a subset of 
set2, if every element in set1 is also in set2.  
</p>

<pre>
$ ./sets2.py 
set([1, 2, 3, 4])
set([1, 2, 3, 4, 6, 7])
Is set1 subset of set2 ? :  True
Is set1 superset of set2 ? :  False
set([])
</pre>

<pre>
 fs = frozenset(['a', 'b', 'c'])
</pre>

<p>
If we need an immutable set, we create a <b>frozenset</b>
</p>


<h2>Dictionaries</h2>

<p>
A Python <b>dictionary</b> is a group of key-value pairs. The elements in a 
dictionary are indexed by keys. Keys in a dictionary are required to be unique. 
Because of the importance of the dictionary data type, a whole chapter
covers the dictionary in this Python tutorial.
</p>

<pre class="code">
#!/usr/bin/python

# dict.py

words = { 'girl': 'Maedchen', 'house': 'Haus', 'death': 'Tod' }

print words['house']

print words.keys()
print words.values()
print words.items()

print words.pop('girl')
print words
words.clear()
print words
</pre>

<p>
Our first example shows some basic usage of the dictionary data type. 
We print a specific value, keys and values of the dictionary. The 
<b>items()</b> method returns a list of dictionarie's 
(key, value) pairs as tuples.
</p>

<pre>
$ ./dict.py 
Haus
['house', 'girl', 'death']
['Haus', 'Maedchen', 'Tod']
[('house', 'Haus'), ('girl', 'Maedchen'), ('death', 'Tod')]
Maedchen
{'house': 'Haus', 'death': 'Tod'}
{}
</pre>


<p>
In this part of the Python tutorial, we have described Python
data types.
</p>


<div class="botNav, center">
<span class="botNavItem"><a href="/">Home</a></span> ‡ <span class="botNavItem"><a href="..">Contents</a></span>  ‡
<span class="botNavItem"><a href="#">Top of Page</a></span>
</div>


<div class="footer">
<div class="signature">
<a href="/">ZetCode</a> last modified March 10, 2012  <span class="copyright">&copy; 2007 - 2013 Jan Bodnar</span>
</div>
</div>

</div> <!-- content -->

</div> <!-- container -->

</body>
</html>
